package com.whzykj.model;

import com.jfinal.plugin.activerecord.Model;
import com.whzykj.jfinal.TableBind;

import java.math.BigDecimal;
import java.util.*;

@TableBind(name = "recharge_activity_details", pk = "id")
public class RechargeActivityDetails extends Model<RechargeActivityDetails>{
	private static final long serialVersionUID = 1L;
	public static final RechargeActivityDetails me = new RechargeActivityDetails();
	
	/**
	 * 根据站点ID查询目前有效的活动规则列表
	 * 业务规则：获取最新的使用于该站点的充值活动
	 * @param stationid  stationid为null值时，查找stationid为-1的活动规则
	 * @param rechargeAmount
	 * @return
	 */
	public List<RechargeActivityDetails> findValidActivityDetails(Integer stationid){
		List<RechargeActivityDetails> list=null;
		if(stationid==null || stationid==0){
			stationid=-1;
		}
		StringBuffer sql=new StringBuffer(" select * from recharge_activity_details rad");
		sql.append(" where rad.activity_id =(select ra.id from recharge_activity ra");
		sql.append(" WHERE ra.activity_state='01' AND TO_DAYS(NOW())>=TO_DAYS(ra.start_time) AND TO_DAYS(NOW())<=TO_DAYS(ra.end_time)");
		sql.append(" ORDER BY ra.start_time DESC");
		sql.append(" limit 1) AND rad.station_id="+stationid);
		sql.append(" order by rad.recharge_amount DESC");
		list=RechargeActivityDetails.me.find(sql.toString());
		return list;
	}
	
	/**
	 * 根据客户的站点ID和充值金额查找匹配的充值规则
	 * @param stationid
	 * @param amount
	 * @return
	 */
	public RechargeActivityDetails findValidRechargeActivityDetails(Integer stationid, Integer amount){
		List<RechargeActivityDetails> list=findValidActivityDetails(stationid);
		if(list==null || list.size()==0){
			list=findValidActivityDetails(-1);	// 如果找不到该站点的信息，则寻找通用站点的活动充值规则
		}
		RechargeActivityDetails rad=null;
		for(RechargeActivityDetails r:list){
			if(amount.intValue()>=r.getBigDecimal("recharge_amount").intValue()){
				rad=r;
				break;
			}
		}
		if(rad==null){		// 如果充值金额小于最小金额，则默认为最小金额充值规则
			rad=list.get(list.size()-1);
		}
		return rad;
	}	
	
	/**
	 * 根据客户的站点ID和充值金额查找匹配的充值规则
	 * @param stationid
	 * @param amount
	 * @return
	 */
	public RechargeActivityDetails balanValidRechargeActivityDetails(Integer stationid, BigDecimal amount){
		List<RechargeActivityDetails> list=findValidActivityDetails(stationid);
		if(list==null || list.size()==0){
			list=findValidActivityDetails(-1);	// 如果找不到该站点的信息，则寻找通用站点的活动充值规则
		}
		RechargeActivityDetails rad=null;
		for(RechargeActivityDetails r:list){
			if(amount.intValue()>=r.getBigDecimal("recharge_amount").intValue()){
				rad=r;
				break;
			}
		}
		if(rad==null){		// 如果充值金额小于最小金额，则默认为最小金额充值规则
			rad=list.get(list.size()-1);
		}
		return rad;
	}
	
	/**
	 * 根据传入的站点ID查找对应的线上充值活动列表
	 * @param stationid
	 * @param amount
	 * @return
	 */
	public List<RechargeActivityDetails> findValidRechargeActivityDetailsList(Integer stationid){
		List<RechargeActivityDetails> list=findValidActivityDetails(stationid);
		if(list==null || list.size()==0){
			list=findValidActivityDetails(-1);	// 如果找不到该站点的信息，则寻找通用站点的活动充值规则
		}
		List<RechargeActivityDetails> recList=new ArrayList<RechargeActivityDetails>();
		if(list!=null && list.size()>0){
			for(int i=list.size()-1;i>=0;i--){
				RechargeActivityDetails rad=list.get(i);
				if(rad.getStr("online_state")!=null && rad.getStr("online_state").trim().equals("00")){
					recList.add(rad);
				}
			}
		}
		return recList;
	}
	
	/**
	 * 根据传入的站点ID查找对应的线上充值活动最大金额
	 * @param stationid
	 * @return
	 */
	public BigDecimal getMaxActivityAmountOnline(Integer stationid){
		List<RechargeActivityDetails> list=findValidRechargeActivityDetailsList(stationid);
		BigDecimal max_online=BigDecimal.ZERO;
		if(list!=null && list.size()>0){
			max_online=list.get(list.size()-1).getBigDecimal("recharge_amount");
		}
		return max_online;
	}
	
}
